package com.youyi.yychosesdk.utils.result;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;

public class EasyResult {
    private static final String TAG = "com.youyi.yychosesdk";

    private EasyResult() {
    }

    public static HolderFragment get(FragmentActivity fragmentActivity) {
        return new EasyResult().getHolderFragment(fragmentActivity.getSupportFragmentManager());
    }

    public static HolderFragment get(Fragment fragment) {
        return new EasyResult().getHolderFragment(fragment.getChildFragmentManager());
    }

    private HolderFragment getHolderFragment(FragmentManager fragmentManager) {
        HolderFragment findHolderFragment = findHolderFragment(fragmentManager);
        if (findHolderFragment != null) {
            return findHolderFragment;
        }
        HolderFragment holderFragment = new HolderFragment();
        fragmentManager.beginTransaction().add(holderFragment, "com.youyi.yychosesdk").commitAllowingStateLoss();
        fragmentManager.executePendingTransactions();
        return holderFragment;
    }

    private HolderFragment findHolderFragment(FragmentManager fragmentManager) {
        return (HolderFragment) fragmentManager.findFragmentByTag("com.youyi.yychosesdk");
    }
}
